<template>
    <div class="loginBox">
        <el-form :model="userInfo" :rules="rules" ref="loginForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="userInfo.username" clearable></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="userInfo.password" clearable show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="login">登录</el-button>
                <el-button @click="reset">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
// 引入辅助性函数
import { mapActions } from "vuex";
// 引入登录接口
import { userLogin } from "../request/api"
export default {
    data() {
        return {
            userInfo: {
                username: "",
                password: ""
            },
            // 表单正则判断
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
                ],
            }
        };
    },
    methods: {
        //点击登录
        login() {
            this.$refs.loginForm.validate(flag => {
                if (flag) {
                    // 调用登录接口
                    userLogin(this.userInfo).then(res => {
                        console.log(res);
                        if (res.code == 200) {
                            // 将管理员信息存储到vuex和本地
                            this.setUserInfo(res.list)
                            //登录成功，跳转到首页
                            this.$router.push("/index")
                            this.$message.success(res.msg)
                        }
                    })
                } else {
                    this.$message.error("请输入正确的内容");
                }
            })
        },
        //点击重置
        reset() {
            this.$refs.loginForm.resetFields()
        },
        // 派遣管理员信息存储行动
        ...mapActions({
            setUserInfo: "setUserInfo_Action"
        })
    }
};
</script>

<style scoped lang="less">
.loginBox {
    width: 100%;
    height: 590px;
    background-color: darkorange;
    padding-top: 150px;

    .el-form {
        margin: auto;
        width: 30%;
        padding: 30px;
        background-color: #fff;
        border-radius: 20px;
    }
}
</style>
